<template>
  <div class="app-container">
    <!-- 表单 -->
    <el-form :inline="true" :model="courseQuery" class="demo-form-inline">
      <!-- 课程一级分类 -->
      <el-form-item label="课程类别">
        <el-select
          v-model="courseQuery.subjectParentId"
          placeholder="请选择"
          @change="subjectParentChange"
        >
          <el-option
            v-for="item in subjectNestedList"
            :key="item.id"
            :label="item.title"
            :value="item.id"
          >
          </el-option>
        </el-select>
        <!-- 课程二级分类 -->
        <el-select placeholder="请选择" v-model="courseQuery.subjectId">
          <el-option
            v-for="item in subjectTwoList"
            :key="item.id"
            :label="item.title"
            :value="item.id"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <!-- 课程标题 -->
      <el-form-item label="">
        <el-input placeholder="课程标题" v-model="courseQuery.title"></el-input>
      </el-form-item>
      <!-- 讲师分类 -->
      <el-form-item label="">
        <el-select placeholder="请选择讲师" v-model="courseQuery.teacherId">
          <el-option
            v-for="item in teacherSearchList"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" @click="getList()"
          >查询</el-button
        >
        <el-button @click="resetData()">清空</el-button>
      </el-form-item>
    </el-form>
    <!-- 列表表格 -->
    <el-table
      v-loading="listLoading"
      :data="list"
      element-loading-text="Loading"
      border
      fit
      highlight-current-row
      row-class-name="myClassList"
    >
      <el-table-column align="center" label="序号" width="80">
        <template slot-scope="scope">
          {{ (page - 1) * limit + scope.$index + 1 }}
        </template>
      </el-table-column>
      <el-table-column label="课程信息" width="300" align="center">
        <template slot-scope="scope">
          <div class="info">
            <div class="pic">
              <img :src="scope.row.cover" :alt="scope.row.title" />
            </div>
            <div class="title">
              <a href="#">{{ scope.row.title }}</a>
              <p style="text-align: left">{{ scope.row.lessonNum }}课时</p>
            </div>
          </div>
        </template>
      </el-table-column>
      <el-table-column label="创建时间" width="100" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.gmtCreate.substr(0, 10) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="发布时间" width="100" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.gmtModified.substr(0, 10) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="价格" width="100" align="center">
        <template slot-scope="scope">
          <!-- toFixed：保留两位小数 -->
          <span>{{
            scope.row.price === 0 ? "免费" : "￥" + scope.row.price.toFixed(2)
          }}</span>
        </template>
      </el-table-column>
      <el-table-column label="付费学员" width="100" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.buyCount }}人</span>
        </template>
      </el-table-column>
      <el-table-column label="课程状态" width="100" align="center">
        <template slot-scope="scope">
          <el-tag :type="scope.row.status == 'Normal' ? 'success' : ''">
            {{ scope.row.status == "Normal" ? "已发布" : "未发布" }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作" width="200" align="center">
        <template slot-scope="scope">
          <router-link :to="`/course/info/${scope.row.id}`">
            <el-button type="primary" size="mini" icon="el-icon-edit"
              >编辑</el-button
            >
          </router-link>
          <el-button
            type="danger"
            size="mini"
            icon="el-icon-delete"
            @click="delList(scope.row.id)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页器 -->
    <el-pagination
      style="text-align: center; margin-top: 20px"
      @current-change="getList"
      :current-page="page"
      :page-size="limit"
      layout="total, prev, pager, next,jumper"
      :total="total"
    >
    </el-pagination>
  </div>
</template>
  
  <script>
import courseApi from "@/api/course";
import subjectApi from "@/api/subject";
import teacherApi from "@/api/teacher";
export default {
  data() {
    return {
      list: null, //当前列表所需要的数据
      listLoading: true, // 加载中的效果
      page: 1, // 当前页
      limit: 10, //每页记录数
      courseQuery: {
        teacherId: "", // 讲师id
        subjectId: "", // 课程分类id
        subjectParentId: "", // 一级课程分类id
        title: "", // 标题
      }, //条件查询对象
      total: 0, // 总记录数
      subjectNestedList: [], // 一级课程分类
      teacherSearchList: [], // 讲师分类
      subjectTwoList: [], // 二级课程分类
    };
  },
  created() {
    // 获取课程列表分页数据
    this.getList();
    // 获取一级课程分类数据
    this.initSubjectList();
    // 调用获取所有讲师方法
    this.getTeacherAll();
  },
  methods: {
    // 默认page=1
    getList(page = 1) {
      // 获取当前页码
      this.page = page;

      //   调用获取讲师列表的用户api
      this.listLoading = true;
      courseApi.getList(this.page, this.limit, this.courseQuery).then((res) => {
        // console.log(res);
        // 获取api中的数据
        this.list = res.data.items;

        // 获取数据中总记录数
        // console.log(res);
        this.total = res.data.totalCount;
        this.limit = res.data.pageSize;

        // 当加载中停止转动
        this.listLoading = false;
      });
    },
    resetData() {
      // 清空查询条件
      this.courseQuery = {};
      // 调用获取讲师列表的用户api
      this.getList();
    },
    // 初始化一级课程列表分类
    initSubjectList() {
      subjectApi.getList().then((res) => {
        this.subjectNestedList = res.data;
      });
    },
    // 所有讲师列表
    getTeacherAll() {
      teacherApi.getTeacherAll().then((res) => {
        this.teacherSearchList = res.data;
      });
    },
    // 课程类别一级分类改变时触发
    subjectParentChange(val) {
      // 根据当前一级分类信息，查看二级分类
      // console.log(val);
      // 找到一级分类下的children
      for (let i = 0; i < this.subjectNestedList.length; i++) {
        if (this.subjectNestedList[i].id == val) {
          this.subjectTwoList = this.subjectNestedList[i].children;
          // 清空查询条件中的二级分类id、
          this.courseQuery.subjectId = "";
        }
      }
    },
    // 根据id删除课程列表
    delList(id) {
      // 根据id进行删除
      // console.log(id);
      // 确认是否删除
      this.$confirm("您确定要删除该课程吗?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "error",
      })
        .then(() => {
          // 点击确认在进行调用teacher.js中的删除api
          courseApi.delList(id).then((res) => {
            // 删除成功
            this.$message({
              type: "success",
              message: "删除成功!",
            });
            this.getList();
          });
          // 重新加载删除后的数据
        })
        .catch(() => {});
    },
  },
};
</script>
  
<style scoped>
.myClassList .info {
  width: 300px;
  overflow: hidden;
}
.myClassList .info .pic {
  width: 150px;
  height: 90px;
  overflow: hidden;
  float: left;
}
.myClassList .info .pic a {
  display: block;
  width: 100%;
  height: 100%;
  margin: 0;
  padding: 0;
}
.myClassList .info .pic img {
  display: block;
  width: 100%;
}
.myClassList td .info .title {
  float: left;
  margin-left: 20px;
  height: 90px;
}
.myClassList td .info .title a {
  display: block;
  height: 48px;
  line-height: 24px;
  overflow: hidden;
  color: #00baf2;
  margin-bottom: 12px;
}
.myClassList td .info .title p {
  line-height: 20px;
  margin-top: 5px;
  color: #818181;
}
</style>